package com.honey.standard.writer.imp;

import java.io.File;
import java.io.IOException;

import com.honey.core.compiler.Compilation;
import com.honey.core.compiler.CompilationDescriptor;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.StorageType;
import com.honey.standard.writer.StandardWriterPlugin;
import com.honey.standard.writer.io.IoUtility;
import com.honey.standard.writer.io.Storage;
import com.honey.standard.writer.io.StorageFactory;

/**
 * 写入文件基类
 * @author Administrator
 *
 */
abstract class BaseFileWriter {
	
	private StandardWriterPlugin standardWriterPlugin = null;
	
	private File target = null;
	
	public BaseFileWriter(StandardWriterPlugin standardWriterPlugin){
		this.standardWriterPlugin = standardWriterPlugin;
	}
	
	/**
	 * 写入文件
	 * @param compilationDescriptor
	 */
	public void writeFile(CompilationDescriptor compilationDescriptor) {
		String charset = standardWriterPlugin.getCharset();
		if(charset == null){
			charset = compilationDescriptor.getCharset();
		}else{
			compilationDescriptor.setCharset(charset);
		}
		
		Compilation compilation = compilationDescriptor.getCompilation();
		File compilationFile =  IoUtility.referenceFile(compilation.getType(), compilationDescriptor.getCompilationType());
		String parent = compilationFile.getParent() ;
		File targetDirectory =parent== null ?  new File( compilationDescriptor.getWorkSpace() ):
											   new File( compilationDescriptor.getWorkSpace(), parent)
								;  
			
			
		if( !targetDirectory.exists() ){
			targetDirectory.mkdirs();
		}
		target =  new File(targetDirectory,compilationFile.getName() );
		Storage storage = null;
		try {
			storage = StorageFactory.getStorage( 
					getStorageType(compilationDescriptor), 
					target ,
					charset,
					compilationDescriptor.isCheckedFeature()
			);
			if( storage.supportWrite() ){
				if( target.exists() ){
					target.setWritable(true);
				}
				storage.write(compilation);
				compilationDescriptor.setCharset(charset);
			
				target = null;
				target = storage.getSourceFile();
				target.setWritable(standardWriterPlugin.isEnableWrite());
				System.out.println( new StringBuilder()
					.append("Compilation File  : ")
					.append(compilationDescriptor.getStorageType() )
					.append(" - " )
					.append(compilation.getType().getPackageName().replace('.', File.separatorChar))
					.append(File.separatorChar)
					.append(target.getName())
				);
			}else{
				System.out.println( new StringBuilder()
					.append("文件验证失败跳过写入  : ")
					.append(compilationDescriptor.getStorageType() )
					.append(" - " )
					.append(compilation.getType().getPackageName().replace('.', File.separatorChar))
					.append(File.separatorChar)
					.append(target.getName())
				);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (CompilationException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 查找存储类型
	 * @param storageType
	 * @return
	 */
	private StorageType getStorageType(CompilationDescriptor compilationDescriptor){
		StorageType answer = null;
		StorageType storageType = compilationDescriptor.getStorageType();
		if( standardWriterPlugin.getStorageType() != null  ){
			answer = standardWriterPlugin.getStorageType();
			compilationDescriptor.setStorageType(answer);
		}else{
			answer = storageType== null ? StorageType.BACKUP_STORAGE : storageType ;
		}
		return answer ;
	}
	
	/**
	 * 获取存储后的目标文件
	 * @return
	 */
	public File getTargetFile() {
		return target;
	}
	
}
